package GUI;
import controller.Controller;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

public class OrderGUI {
    Controller controller = new Controller();

    OrderGUI() {
        insert();
    }
    private void insert() {
        JFrame frame = new JFrame();
        frame.setResizable(false);
        //	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());

        Vector<String> columnNames = createColumnNames();
        Vector data = createData();//初始数据
        DefaultTableModel defaultModel = new DefaultTableModel(data,columnNames){
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };//表格不可编

        JTable table = new JTable(defaultModel);
        table.setFont(new Font("宋体",Font.PLAIN,20));
        table.getTableHeader().setFont(new Font("宋体",Font.PLAIN,20));
        table.setRowHeight(25);
        table.getTableHeader().setReorderingAllowed(false);
        JScrollPane scrollPane = new JScrollPane(
                table,
                ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER
        );
        //使表格内容居中
        DefaultTableCellRenderer dtcr = new DefaultTableCellRenderer();
        dtcr.setHorizontalAlignment(JLabel.CENTER);
        table.setDefaultRenderer(Object.class,dtcr);
        frame.add(scrollPane,BorderLayout.CENTER);

        JPanel panel = new JPanel(new GridLayout(2,2));
        JLabel label1 = new JLabel("总价：");
        JLabel label2 = new JLabel();//计算出总的金额
        label2.setText(null);
        label1.setFont(new Font("宋体",Font.PLAIN,20));
        label2.setFont(new Font("宋体",Font.PLAIN,20));
        //总价计算
        int rowCount=defaultModel.getRowCount();
        double count=0;
        for(int i=0;i<rowCount;i++)
        {
            double priceCount = (double) defaultModel.getValueAt(i,4);
            count+=priceCount;
        }
        //***********
        label2.setText(String.valueOf(count)+"元");
        JButton button1 = new JButton("确认订单");
        JButton button2 = new JButton("取消订单");
        button1.setFont(new Font("宋体",Font.PLAIN,20));
        button1.setSize(200,10);
        button2.setFont(new Font("宋体",Font.PLAIN,20));
        button2.setSize(200,10);
        panel.add(label1);
        panel.add(label2);
        panel.add(button1);
        panel.add(button2);
        frame.add(panel,BorderLayout.SOUTH);
        //确认按钮触发器
        button1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                orderJFrame();
                frame.setVisible(false);
            }
        });
        button2.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                frame.setVisible(false);
            }
        });

        frame.pack();
        frame.setVisible(true);
        frame.setBounds((Toolkit.getDefaultToolkit().getScreenSize().width)/2-300, (Toolkit.getDefaultToolkit().getScreenSize().height/2)-400, 500,600);

    }
    private Vector<String> createColumnNames()
    {
        Vector<String> vName = new Vector<>();
        vName.add("商品编号");
        vName.add("商品名称");
        vName.add("单价(元）");
        vName.add("数量");
        vName.add("总价");
        return vName;
    }
    private Vector createData()
    {
        ArrayList<String> cartArrayList = controller.getCartArrayList();
        if(cartArrayList.get(0).contains("null")){
            cartArrayList.remove(0);
        }
        Vector cartData = new Vector<>();

        for(int j = 0; j< cartArrayList.size(); j++)
        {
            Vector cartsData = new Vector<>();
            String[] temp = cartArrayList.get(j).split(" ");
            String[] data = {temp[0], temp[1], temp[2], temp[3]};
            cartsData.add(temp[0]);//编号
            cartsData.add(temp[1]);//名称
            cartsData.add(temp[2]);//价格
            double price = Double.parseDouble(temp[2]);
            int num = Integer.parseInt(temp[3]);
            double totalPrice = price*num;
            cartsData.add(num);
            cartsData.add(totalPrice);//总价
            cartData.add(cartsData);
        }
        return cartData;
    }
    private void orderJFrame()
    {
        JFrame orderFrame = new JFrame("订单确认");
        JPanel orderPanel = new JPanel(new	GridLayout(1,2));
        ImageIcon icon = new ImageIcon("photo1.jpg");
        ImageIcon icon1 = new ImageIcon("photo2.jpg");
        icon.setImage(icon.getImage().getScaledInstance(400, 450, 100));
        icon1.setImage(icon1.getImage().getScaledInstance(400, 450, 100));
        JLabel photoLabel1 = new JLabel(icon);
        JLabel photoLabel2 = new JLabel(icon1);
        orderPanel.add(photoLabel1);
        orderPanel.add(photoLabel2);
        orderFrame.add(orderPanel,BorderLayout.CENTER);
        JPanel orderPanel1 = new JPanel();
        JButton button = new JButton("订单已支付");
        button.setFont(new Font("宋体",Font.PLAIN,20));
        button.setSize(500,50);
        orderPanel1.add(button);
        orderFrame.add(orderPanel1,BorderLayout.SOUTH);
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                button.setText("订单创建中....");
                String str = controller.settlementShoppingCart();
                if(!str.equals("结算成功")){
                    JOptionPane.showMessageDialog(null, str);
                }

            }
        });

        orderFrame.pack();
        orderFrame.setVisible(true);
        orderFrame.setBounds((Toolkit.getDefaultToolkit().getScreenSize().width)/2-250, (Toolkit.getDefaultToolkit().getScreenSize().height/2)-300, 800,600);

    }
}
